package com.fyy.im.server.handler;

import com.fyy.im.message.GJRequestMessage;
import com.fyy.im.message.GJResponseMessage;
import com.fyy.im.server.session.Group;
import com.fyy.im.server.session.GroupSessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @Author fyy
 * @Description
 * @Date 2021/5/12 15:21
 */
@ChannelHandler.Sharable
public class GroupJoinRequestMessageHandler extends SimpleChannelInboundHandler<GJRequestMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GJRequestMessage msg) throws Exception {
        Group group = GroupSessionFactory.getGroupSession().joinMember(msg.getGroupName(), msg.getUsername());
        if(null != group){
            ctx.writeAndFlush(new GJResponseMessage(true,"您已加入"+msg.getGroupName()));
        }else{
            ctx.writeAndFlush(new GJResponseMessage(false,msg.getGroupName()+"不存在!"));
        }

    }

}
